﻿#region Vương văn hùng
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GUI.NhaCungCap
{
    //hungvv xu ly
    public partial class frmXuLy : Form
    {
        #region Delegate
        public delegate void GetString(Boolean capnhat);
        public GetString GetData;
        public static bool CheckClose = false;
        #endregion

        #region Khoi Tao
        private Entities.ChiTietHangHoaNhaCungCap[] DanhSach = null;
        private DateTime date;
        private string MaNhaCungCap = string.Empty;
        private string links = string.Empty;
        private string linksRoot = string.Empty;
        private Image image = null;
        private byte[] bytes = null;
        private Entities.ChiTietHangHoaNhaCungCap banghi = null;
        private Entities.HangHoa[] tableHangHoa;
        public frmXuLy()
        {
            InitializeComponent();
        }
        private string hanhDong;
        public string HanhDong
        {
            get { return hanhDong; }
            set { hanhDong = value; }
        }
        private Entities.NhaCungCap nhacungcap;

        public Entities.NhaCungCap Nhacungcap
        {
            get { return nhacungcap; }
            set { nhacungcap = value; }
        }

        public frmXuLy(string hanhDong, Entities.NhaCungCap nhacungcap)
        {
            InitializeComponent();
            this.hanhDong = hanhDong;
            this.nhacungcap = nhacungcap;
        }
        #endregion

        #region Hien Thi Gridview
        private void Gridview()
        {
            try
            {
                if (DanhSach == null)
                {
                    Entities.ChiTietHangHoaNhaCungCap[] nv = new Entities.ChiTietHangHoaNhaCungCap[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["MaHangHoa"].Visible = true;
                dgvHienThi.Columns["MaHangHoa"].HeaderText = "Mã hàng hóa";
                dgvHienThi.Columns["TenHangHoa"].Visible = true;
                dgvHienThi.Columns["TenHangHoa"].HeaderText = "Tên hàng hóa";
                dgvHienThi.Columns["TenDonViTinh"].Visible = true;
                dgvHienThi.Columns["TenDonViTinh"].HeaderText = "Đơn vị tính";
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        #endregion

        #region Load
        private void frmXuLy_Load(object sender, EventArgs e)
        {
            try
            {
                this.WindowState = FormWindowState.Normal;
                try
                {
                    date = Common.StaticVariable.InterfaceObject.SelectDate();
                }
                catch (Exception)
                {
                    date = DateTime.Now;
                }
                Select();
                switch (this.hanhDong)
                {
                    case "Them":
                        try
                        {
                            this.MaNhaCungCap = GetID();
                        }
                        catch (Exception) { }
                        break;
                    case "Sua":
                        DoDuLieu();
                        break;
                    default:
                        this.Thoat();
                        break;
                }
                this.Gridview();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua Du Lieu
        private void DoDuLieu()
        {
            try
            {
                this.MaNhaCungCap = this.nhacungcap.MaNhaCungCap;
                txtTenNhaCungCap.Text = this.nhacungcap.TenNhaCungCap;
                txtNoHienThoi.Text = this.nhacungcap.NoHienThoi;
                txtDienThoai.Text = this.nhacungcap.DienThoai;
                txtEmail.Text = this.nhacungcap.Email;
                txtWeb.Text = this.nhacungcap.Website;
                txtFax.Text = this.nhacungcap.Fax;
                txtDiaChi.Text = this.nhacungcap.DiaChi;
                txtNguoiLienHe.Text = this.nhacungcap.NguoiLienHe;
                txtMaSoThue.Text = this.nhacungcap.MaSoThue;
                txtGhiChu.Text = this.nhacungcap.GhiChu;
                txtLinkImage.Text = this.nhacungcap.LinkImageRoot;
                this.links = this.nhacungcap.LinkImage;
                this.linksRoot = this.nhacungcap.LinkImageRoot;
                if (this.nhacungcap.LinkImageRoot != string.Empty)
                {
                    try
                    {
                        bytes = Common.StaticVariable.InterfaceObject.GetImage(nhacungcap.LinkImageRoot);
                    }
                    catch (Exception)
                    {
                        bytes = null;
                    }
                    if (bytes != null)
                    {
                        image = Common.Utilities.GetImage(bytes);
                        pictureBox1.BackgroundImage = image;
                    }
                    else
                    {
                        bytes = Common.Utilities.GetImage(System.Windows.Forms.Application.StartupPath.ToString() + @"\Default.bmp");
                        image = Common.Utilities.GetImage(bytes);
                        pictureBox1.BackgroundImage = image;
                    }
                }
                else
                {
                    bytes = Common.Utilities.GetImage(System.Windows.Forms.Application.StartupPath.ToString() + @"\Default.bmp");
                    image = Common.Utilities.GetImage(bytes);
                    pictureBox1.BackgroundImage = image;
                }
                this.DanhSach = this.ChiTietNhaCungCap(this.nhacungcap.MaNhaCungCap);
                dgvHienThi.DataSource = this.DanhSach;
                this.Gridview();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Lay Ma
        private string GetID()
        {
            string code = string.Empty;
            try
            {
                Entities.GetID id = new Entities.GetID();
                id.TableName = "NhaCungCap";
                Entities.GetID get = Common.StaticVariable.InterfaceObject.SelectID(id);
                if (get.ColumnName != null)
                {
                    code = Common.Utilities.ProcessID(get.ColumnName);
                }
                else
                { code = "NCC_0001"; }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                code = "NCC_0001";
            }
            return code;
        }
        #endregion

        #region Chi tiet nha cung cap
        private Entities.ChiTietHangHoaNhaCungCap[] ChiTietNhaCungCap(string manhacungcap)
        {
            Entities.ChiTietHangHoaNhaCungCap[] chitiet = null;
            try
            {
                Entities.ChiTietHangHoaNhaCungCap id = new Entities.ChiTietHangHoaNhaCungCap();
                id.HanhDong = "4";
                id.MaNhaCungCap = manhacungcap;
                id.MaHangHoa = "";
                chitiet = Common.StaticVariable.InterfaceObject.SelectChiTietHangHoaNhaCungCap(id);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                chitiet = null;
            }
            return chitiet;
        }
        #endregion

        #region Quyen
        private Boolean Quyen()
        {
            return true;
        }
        #endregion

        #region Gia Tri Them
        private Entities.NhaCungCap LayBanGhi(string hanhdong)
        {
            Entities.NhaCungCap ncc = new Entities.NhaCungCap();
            try
            {
                ncc.HanhDong = hanhdong;
                ncc.MaNhaCungCap = MaNhaCungCap;
                ncc.TenNhaCungCap = txtTenNhaCungCap.Text;
                ncc.NoHienThoi = txtNoHienThoi.Text;
                string sdt = txtDienThoai.Text;
                if (sdt == "   -   -")
                {
                    sdt = string.Empty;
                }
                ncc.DienThoai = sdt;
                ncc.Email = txtEmail.Text;
                ncc.Website = txtWeb.Text;
                ncc.Fax = txtFax.Text;
                ncc.DiaChi = txtDiaChi.Text;
                ncc.NguoiLienHe = txtNguoiLienHe.Text;
                ncc.MaSoThue = txtMaSoThue.Text;
                ncc.GhiChu = txtGhiChu.Text;
                ncc.Deleted = false;
                ncc.DanhSachHangHoa = this.DanhSach;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                ncc = null;
            }
            return ncc;
        }
        #endregion

        #region Validate
        private Boolean Validate(Entities.NhaCungCap ncc)
        {
            Boolean kt = true;
            try
            {
                if (ncc.MaNhaCungCap == string.Empty)
                {
                    MessageBox.Show("Không có mã nhà cung cấp","Thông báo");
                    return false;
                }
                if (ncc.TenNhaCungCap == string.Empty)
                {
                    MessageBox.Show("Không có tên nhà cung cấp", "Thông báo");
                    return false;
                }
                if (ncc.DienThoai == string.Empty)
                {
                    MessageBox.Show("Không có số điện thoại", "Thông báo");
                    return false;
                }
                if (ncc.DiaChi == string.Empty)
                {
                    MessageBox.Show("Không có địa chỉ", "Thông báo");
                    return false;
                }
                if (ncc.NguoiLienHe == string.Empty)
                {
                    MessageBox.Show("Không có người liên hệ", "Thông báo");
                    return false;
                }
                for (int i = 0; i < ncc.DanhSachHangHoa.Length; i++)
                {
                    ncc.DanhSachHangHoa[i].HanhDong = ncc.HanhDong;
                    ncc.DanhSachHangHoa[i].MaNhaCungCap = ncc.MaNhaCungCap;
                    ncc.DanhSachHangHoa[i].TenNhaCungCap = ncc.TenNhaCungCap;
                    ncc.GhiChu = ncc.GhiChu;
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                kt = false;
            }
            return kt;
        }
        #endregion

        #region Tim Anh
        //private void Search()
        //{
        //    try
        //    {
        //        OpenFileDialog open = new OpenFileDialog();
        //        if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //        {
        //            links = open.SafeFileName.ToString();
        //            linksRoot = open.FileName.ToString();
        //            bytes = Common.Utilities.GetImage(linksRoot);
        //            image = Common.Utilities.GetImage(bytes);
        //            picNhaCungCap.BackgroundImage = image;
        //            txtLinkRoot.Text = linksRoot;
        //            picNhaCungCap.BackgroundImageLayout = ImageLayout.Stretch;
        //        }
        //        else
        //        {
        //            links = this.NhaCungCap.LinkImage;
        //            linksRoot = this.NhaCungCap.LinkImageRoot;
        //            txtLinkRoot.Text = this.NhaCungCap.LinkImage;
        //            bytes = this.NhaCungCap.Images;
        //            image = this.NhaCungCap.Picture;
        //        }
        //        txtSoChungMinhThu.Focus();
        //    }
        //    catch (Exception ex)
        //    {
        //        string s = ex.Message;
        //    }
        //}
        #endregion

        #region Tim hang hoa
        private void Enter(string MaHangHoa)
        {
            try
            {
                if (MaHangHoa == string.Empty)
                {
                    MessageBox.Show("Bạn phải nhập mã hàng hóa", "Thông báo");
                    return;
                }
                if (tableHangHoa == null)
                {
                    MessageBox.Show("Không có hàng hóa", "Thông báo");
                    return;
                }
                for (int i = 0; i < tableHangHoa.Length; i++)
                {
                    if (tableHangHoa[i].MaHangHoa.ToUpper() == MaHangHoa.ToUpper())
                    {
                        banghi = ChuyenDoi(tableHangHoa[i]);
                        break;
                    }
                    else
                    { continue; }
                }
                if (Common.StaticVariable.hanghoa != null)
                {
                    txtMaHangHoa.Text = Common.StaticVariable.hanghoa.MaHangHoa;
                    txtTenHangHoa.Text = Common.StaticVariable.hanghoa.TenHangHoa;
                    txtMaDonViTinh.Text = Common.StaticVariable.hanghoa.MaDonViTinh;
                    txtTenDonViTinh.Text = Common.StaticVariable.hanghoa.TenDonViTinh;
                    banghi = ChuyenDoi(Common.StaticVariable.hanghoa);
                    btnThem.Focus();
                    return;
                }
                else
                {
                    MessageBox.Show("Không tìm thấy hàng hóa này", "Thông báo");
                    txtMaHangHoa.Text = "<F4 - Tra cứu>";
                    txtTenHangHoa.Text = "";
                    txtMaDonViTinh.Text = "";
                    txtTenDonViTinh.Text = "";
                    banghi = null;
                    txtMaHangHoa.Focus();
                    return;
                }
            }
            catch (Exception)
            {

            }
        }
        private Entities.ChiTietHangHoaNhaCungCap ChuyenDoi(Entities.HangHoa row)
        {
            Entities.ChiTietHangHoaNhaCungCap giatri = null;
            try
            {
                giatri = new Entities.ChiTietHangHoaNhaCungCap();
                giatri.MaNhaCungCap = string.Empty;
                giatri.TenNhaCungCap = string.Empty;
                giatri.MaHangHoa = row.MaHangHoa;
                giatri.TenHangHoa = row.TenHangHoa;
                giatri.MaDonViTinh = row.MaDonViTinh;
                giatri.TenDonViTinh = row.TenDonViTinh;
                giatri.GhiChu = "";
                giatri.Deleted = false;
            }
            catch (Exception)
            {
                giatri = null;
            }
            return giatri;
        }

        private void Select()
        {
            try
            {
                Entities.HangHoa hh = new Entities.HangHoa();
                hh.HanhDong = "1";
                hh.MaHangHoa = "";
                this.tableHangHoa = Common.StaticVariable.InterfaceObject.Select_HangHoaAll(hh);
            }
            catch (Exception ex)
            {
                tableHangHoa = null;
                string s = ex.Message;
            }
        }

        private void TimHangHoa()
        {
            try
            {
                GUI.TimKiem.frmHangHoaNhaCungCap frm = new TimKiem.frmHangHoaNhaCungCap("NhaCungCap");
                frm.ShowDialog();
                if (Common.StaticVariable.hanghoa != null)
                {
                    txtMaHangHoa.Text = Common.StaticVariable.hanghoa.MaHangHoa;
                    txtTenHangHoa.Text = Common.StaticVariable.hanghoa.TenHangHoa;
                    txtMaDonViTinh.Text = Common.StaticVariable.hanghoa.MaDonViTinh;
                    txtTenDonViTinh.Text = Common.StaticVariable.hanghoa.TenDonViTinh;
                    banghi = ChuyenDoi(Common.StaticVariable.hanghoa);
                    btnThem.Focus();
                }
                else
                {
                    txtMaHangHoa.Text = "<F4 - Tra cứu>";
                    txtTenHangHoa.Text = "";
                    txtMaDonViTinh.Text = "";
                    txtTenDonViTinh.Text = "";
                    banghi = null;
                    txtMaHangHoa.Focus();
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Add
        private void AddRow(Entities.ChiTietHangHoaNhaCungCap row)
        {
            try
            {
                ArrayList list = new ArrayList();
                if (DanhSach == null)
                {
                    list.Add(row);
                }
                else
                {
                    for (int i = 0; i < DanhSach.Length; i++)
                    {
                        Entities.HangHoa r = new Entities.HangHoa();
                        if (DanhSach[i].MaHangHoa.ToUpper() != row.MaHangHoa.ToUpper())
                        {
                            list.Add(DanhSach[i]);
                        }
                        else
                        { continue; }
                    }
                    list.Add(row);
                }
                int k = list.Count;
                DanhSach = new Entities.ChiTietHangHoaNhaCungCap[k];
                switch (k)
                {
                    case 0:
                        break;
                    default:
                        for (int i = 0; i < k; i++)
                        {
                            DanhSach[i] = (Entities.ChiTietHangHoaNhaCungCap)list[i];
                        }
                        break;
                }
                dgvHienThi.DataSource = this.DanhSach;
                this.Gridview();
                txtMaHangHoa.Text = "<F4 - Tra cứu>";
                txtTenHangHoa.Text = "";
                txtMaDonViTinh.Text = "";
                txtTenDonViTinh.Text = "";
                banghi = null;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                DanhSach = null;
                this.Gridview();
            }
        }
        #endregion

        #region Remove
        private void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            try
            {
                if (banghi != null)
                {
                    ArrayList list = new ArrayList();
                    for (int i = 0; i < DanhSach.Length; i++)
                    {
                        if (DanhSach[i].MaHangHoa != banghi.MaHangHoa)
                        {
                            list.Add(DanhSach[i]);
                        }
                    }
                    int k = list.Count;
                    if (k == 0)
                    {
                        DanhSach = null;
                    }
                    else
                    {
                        DanhSach = new Entities.ChiTietHangHoaNhaCungCap[k];
                        for (int i = 0; i < k; i++)
                        {
                            DanhSach[i] = (Entities.ChiTietHangHoaNhaCungCap)list[i];
                        }
                    }
                    dgvHienThi.DataSource = DanhSach;
                    Gridview();
                }
            }
            catch (Exception)
            {
            }
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            try
            {
                if (dgvHienThi.RowCount > 0)
                {
                    this.banghi = (Entities.ChiTietHangHoaNhaCungCap)dgvHienThi.CurrentRow.DataBoundItem;

                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region Them
        private void Them()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền thêm", "Thông báo");
                    return;
                }

                Entities.NhaCungCap ncc = this.LayBanGhi("Insert");
                this.MaNhaCungCap = this.GetID();
                if (ncc.MaNhaCungCap != this.MaNhaCungCap)
                {
                    ncc = this.LayBanGhi("Insert");
                }
                if (this.Validate(ncc) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertUpdateNhaCungCap(ncc);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = true;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Sua
        private void Sua()
        {
            try
            {
                if (this.Quyen() != true)
                {
                    MessageBox.Show("Bạn không có quyền sửa", "Thông báo");
                    return;
                }
                Entities.NhaCungCap ncc = this.LayBanGhi("Update");
                //this.MaNhanVien = this.GetID();
                //if (nv.MaNhanVien != this.MaNhanVien)
                //{
                //    nv = this.LayBanGhi("Insert");
                //}
                if (this.Validate(ncc) == false)
                {
                    return;
                }
                int k = Common.StaticVariable.InterfaceObject.InsertUpdateNhaCungCap(ncc);
                if (k == 0)
                {
                    MessageBox.Show("Thất bại hãy liện hệ với nhà quản trị", "Thông báo");
                    return;
                }
                CheckClose = false;
                GetData(true);
                this.Close();
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        #endregion

        #region Event
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Thoat();
        }

        private void btnGhiLai_Click(object sender, EventArgs e)
        {
            try
            {
                if (DanhSach == null)
                {
                    MessageBox.Show("Không có hàng hóa của nhà cung cấp", "Thông báo");
                    return;
                }
                if (DanhSach.Length <= 0)
                {
                    MessageBox.Show("Không có hàng hóa của nhà cung cấp","Thông báo");
                    return;
                }
                switch (this.hanhDong)
                {
                    case "Them":
                        this.Them();
                        break;
                    case "Sua":
                        this.Sua();
                        break;
                }
            }
            catch (Exception)
            {}
        }

        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            { 
                case Keys.F4:
                    this.TimHangHoa();
                    break;
                case Keys.Enter:
                    this.Enter(txtMaHangHoa.Text);
                    break;
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                if (banghi == null)
                {
                    MessageBox.Show("Bạn phải chọn hàng hóa", "Thông báo");
                    txtMaHangHoa.Focus();
                    return;
                }
                this.AddRow(this.banghi);
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }

        private void txtMaHangHoa_Click(object sender, EventArgs e)
        {
            banghi = null;
            txtMaHangHoa.Text = "";
            txtTenHangHoa.Text = "";
            txtMaDonViTinh.Text = "";
            txtTenDonViTinh.Text = "";
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        CheckClose = true;
                        GetData(false);
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion
    }
}
#endregion
